#!/bin/bash
echo 'begin...'
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
DATA_DIR=$DEPLOY_DIR/data
if [ ! -d $DATA_DIR ]; then
    mkdir $DATA_DIR
fi
DOCKER_JAVA_OPTS=""
if [ ! -z "$DOCKER_HOST" ]; then
  echo "配置host:注册的主机ip=$DOCKER_HOST"
  DOCKER_JAVA_OPTS="$DOCKER_JAVA_OPTS -DDUBBO_IP_TO_REGISTRY=$DOCKER_HOST"
fi
if [ ! -z "$DOCKER_PORT" ]; then
    echo "注册的端口号:$DOCKER_PORT"
    DOCKER_JAVA_OPTS="$DOCKER_JAVA_OPTS -DDUBBO_PORT_TO_REGISTRY=$DOCKER_PORT"
    echo "docker端口和注册端口保证一致"
    sed -i "s#^dubbo.port=.*#dubbo.port=$DOCKER_PORT#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/pmsuite-monitor-app.properties;
fi
echo "JVM参数：$DOCKER_JAVA_OPTS"
export DOCKER_JAVA_OPTS
if [ ! -z "$ZOOKEEPER_ADDRESS" ]; then
    sed -i "s#^dubbo.registry.address=.*#dubbo.registry.address=$ZOOKEEPER_ADDRESS#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/pmsuite-monitor-app.properties;
fi
if [ -z "$DB_URL" ]; then
    echo "数据库连接配置无效，pwd为空"
    exit 0;
fi
if [ -z "$DB_UID" ]; then
    echo "数据库连接配置无效，pwd为空"
    exit 0;
fi
if [ -z "$DB_PWD" ]; then
    echo "数据库连接配置无效，pwd为空"
    exit 0;
fi
echo "数据库连接字符串: $DB_URL"
sed -i "s#^monitor.connection.url=.*#monitor.connection.url=$DB_URL#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/pmsuite-monitor-app.properties;
echo "数据库连接用户名: $DB_UID"
sed -i "s#^monitor.connection.username=.*#monitor.connection.username=$DB_UID#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/pmsuite-monitor-app.properties;
echo "数据库连接密码: $DB_PWD"
sed -i "s#^monitor.connection.password=.*#monitor.connection.password=$DB_PWD#g" $DEPLOY_DIR/webapps/ROOT/WEB-INF/classes/pmsuite-monitor-app.properties;
echo -e "Starting  ...\c"
catalina.sh run